<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Tob"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Group" %>
<%@page import="db.Purmat" %>
<%@page import="db.TobDAO" %>
<%@page import="db.PlaceDAO" %>
<%@page import="db.Project" %>
<%@page import="db.Place" %>
<%@page import="db.PlaceDAO" %>
<%@page import="db.UserDAO" %>
<%@page import="db.User"%>
<%@page import="db.Substore"%>
<%@page import="db.SubstoreDAO"%>
<%@page import="db.Fororder"%>
<%@page import="db.FororderDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Purmat"%>
<%@page import="db.Tmppur"%>
<%@page import="db.TmppurDAO"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.Receiveorder"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDAO = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	FororderDAO fororderDAO = (FororderDAO)ctx.getBean("FororderDAO");
	PlaceDAO placeDAO = (PlaceDAO)ctx.getBean("PlaceDAO");
	TmppurDAO tmppurDAO = (TmppurDAO)ctx.getBean("TmppurDAO");
	PurmatDAO purmatDAO = (PurmatDAO)ctx.getBean("PurmatDAO");
	String orderCode = request.getParameter("orderCode");//采购订单编号
	String receiveDate = request.getParameter("receiveDate");//接收日期
	String recode = request.getParameter("recode");//接收单单号
	
	String groupname = request.getParameter("groupname");//接收单位
	String supplier = request.getParameter("supplier");//供应商
	String tobname = request.getParameter("tobname");//业务类型
	String subname = request.getParameter("subname");//业务类型
	String procode = request.getParameter("procode");//项目编码
	String orderremark = request.getParameter("orderremark");//备注
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	Receiveorder receiveorder =new Receiveorder();
	
	boolean success = true;
	String message = "";
	if(orderCode!=null){
		receiveorder.setTmpCode(orderCode);
	}else{
		success =false;
		message = "入库失败，采购订单编号不能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	
	if(receiveDate!=null){
		Date da = sdf.parse(receiveDate);
		receiveorder.setReceiverDate(da);
	}else{
		success =false;
		message = "入库失败，接收日期不能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	if(recode!=null){
		receiveorder.setCode(recode);
	}else{
		success =false;
		message = "入库失败，接收单单号不能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	if(supplier!=null){
		receiveorder.setSupplier(supplier); 
	}else{
		success =false;
		message = "入库失败，供应商不能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	if(session.getAttribute("id")!=null){
		int uid = (Integer)session.getAttribute("id");
		receiveorder.setReceiverUserId(uid);
	}
	if(tobname!=null){
		receiveorder.setTopType(tobname);
	}else{
		success =false;
		message = "入库失败，业务类型能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	if(subname!=null){
		receiveorder.setSubStore(Integer.parseInt(subname));
	}else{
		success =false;
		message = "入库失败，接收子库不能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	if(procode!=null){
		receiveorder.setProCode(procode);
	}else{
		success =false;
		message = "入库失败，项目编码不能为空";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	if(orderremark!=null){
		receiveorder.setRemark(orderremark);
	}else{
		receiveorder.setRemark("");
	}
	int tid = 0;
	tid = (Integer)session.getAttribute("tid");
	int userid=0;
	userid = (Integer)session.getAttribute("id");
	receiveorder.setTeamId(tid);
	receiveorder.setDate(new Date());
	receiveorder.setIsDelete(0);
	receiveorder.setReceiveCompany(groupname);
	receiveorderDAO.save(receiveorder);
	int receid = receiveorder.getId();
	receiveorder.setRcode("R"+receid);
	receiveorderDAO.attachDirty(receiveorder);
	String dtype = request.getParameter("type");
	
	if(receid!=0){
		
		
		String[] mcode = request.getParameterValues("mcode");
		String[] midlist = request.getParameterValues("midlist");//2013年修改。增加接收物资id。以便入库后修改入库数量。如果所有id入库数为零就将入库单进行无效设置。
		String[] mbatch = request.getParameterValues("mbatch");//批次
		String[] kuwei = request.getParameterValues("kuwei");//库位
		String[] mprocode = request.getParameterValues("mprocode");//项目编码
		String[] ordernum =request.getParameterValues("ordernum");//物资申请数量
		String[] mordernum =request.getParameterValues("mordernum");//接收数量
		String[] orderprice = request.getParameterValues("orderprice");//单价
		
		if(mcode!=null && mcode.length>0){
			for(int i=0;i<mcode.length;i++){
				if(!"0".equals(mordernum[i]) ){//接收数量如果为0就不做任何处理2013/4/8
				
				Receivepurmat receivepurmat = new Receivepurmat();
				receivepurmat.setBatch(mbatch[i]);
				receivepurmat.setDate(new Date());
				receivepurmat.setSubStore(Integer.parseInt(subname));
				receivepurmat.setTeamId(tid);
				
				
				//////////////重要/////////////////////
				if(mordernum[i]!=null){
					receivepurmat.setImportNum(Integer.parseInt(mordernum[i]));
					receivepurmat.setStoreNum(Integer.parseInt(mordernum[i]));
					receivepurmat.setUseNum(Integer.parseInt(mordernum[i]));
				}else{
					receivepurmat.setImportNum(0);
					receivepurmat.setStoreNum(0);
					receivepurmat.setUseNum(0);
				}
				/////////////在设置完成后必须增加总库的数量///////////////
				//System.out.println("===============>"+midlist[i]);
				if(midlist[i]!=null){
					Purmat purmat = purmatDAO.findById(Integer.parseInt(midlist[i]));
					
					if(purmat.getWareNum()>0){
						
						int wareNum = purmat.getWareNum();
						purmat.setWareNum(wareNum-Integer.parseInt(mordernum[i]));
						purmatDAO.attachDirty(purmat);
					}
				}
				
				
				
				
				
				//通过物资编码找物资id
				List mlist = materialDAO.findByCode(mcode[i]);
				
				if(mlist!=null && mlist.size()>0){
					for(int m=0;m<mlist.size();m++){
						Material material = new Material();
						material  =(Material)mlist.get(m);
						if(material!=null && material.getIsDelete()==0){
							int mid = material.getId();
							receivepurmat.setMaterialId(mid);
							
							/////////找到对应的物资id。并给总库增加相应数量。
							
							////////后续设置完成总库后增加。
							
							
						}else{
							//没有找到物资id导入失败
							//进行回退
							
							receiveorderDAO.delete(receiveorder);
							//删除物质表中物资id
							List rl = receivepurmatDAO.findByReceiveOrderId(receid);
							for(int r=0;r<rl.size();r++){
								receivepurmatDAO.delete((Receivepurmat)rl.get(r));
							}
							
							///至订单为有效状态。。。。。
							///dtype判断是哪个库。临时还是正式
							if(dtype!=null && (dtype =="0" || "0".equals(dtype))){
								//临时库
								List tlist = tmppurDAO.findByTmpCode(orderCode);
								if(tlist!=null && tlist.size()>0){
									for(int tl = 0;tl<tlist.size();tl++){
										Tmppur tmppur = (Tmppur)tlist.get(tl);
										tmppur.setState(1);//未导入过状态
										tmppurDAO.attachDirty(tmppur);
									}
								}
							}else{
								//正式库
								List flist = fororderDAO.findByOrderCode(orderCode);
								if(flist!=null && flist.size()>0){
									for(int tl = 0;tl<flist.size();tl++){
										Fororder fororder = (Fororder)flist.get(tl);
										fororder.setIsConvert(0);//未导入状态
										fororderDAO.attachDirty(fororder);
									}
								}
							}
							
							
							
							success =false;
							message = "入库失败，物资编码不存在";
							request.setAttribute("message",message);
							request.setAttribute("success",success);
							pageContext.forward("/vm/procure/procureEnd.vm");
							return;
						}
					}
				}
				
				
				
				if(kuwei[i]!=null){
					//System.out.println("========================================================>kuwei="+kuwei[i]);
					receivepurmat.setPlaceId(Integer.parseInt(kuwei[i]));
				}else{
					receivepurmat.setPlaceId(0);
				}
				
				receivepurmat.setProCode(mprocode[i]);
				if(ordernum[i]!=null){
					receivepurmat.setPurNum(Integer.parseInt(ordernum[i]));
				}else{
					receivepurmat.setPurNum(0);
				}
				
				if(orderprice[i]!=null){
					receivepurmat.setPurPrice(Float.parseFloat(orderprice[i]));
				}else{
					receivepurmat.setPurPrice(Float.parseFloat("0"));
				}
				receivepurmat.setReceiveOrderId(receid);
				receivepurmat.setUserId(userid);
				receivepurmat.setIsDelete(0);
				receivepurmatDAO.save(receivepurmat);
				}
			}
		}
		///dtype判断是哪个库。临时还是正式
		if(dtype!=null && (dtype =="0" || "0".equals(dtype))){
			//临时库
			List tlist = tmppurDAO.findByTmpCode(orderCode);
			if(tlist!=null && tlist.size()>0){
				for(int tl = 0;tl<tlist.size();tl++){
					Tmppur tmppur = (Tmppur)tlist.get(tl);
					//2012-12-26由于之前需求不明确，入库需求方希望是多次，但程序结构不支持，所以修改成入库后不进行失效处理，由使用人员进行多次抄作。
					
					List listpumat = purmatDAO.findByTmppurId(tmppur.getId());
					if(listpumat!=null && listpumat.size()>0){
						int jsnum=0;
						for(int pi=0;pi<listpumat.size();pi++){
							Purmat purmat = (Purmat)listpumat.get(pi);
							if(purmat.getWareNum()>0){
								jsnum=jsnum+1;
							}
						}
						if(jsnum==0){
							tmppur.setState(2);//如果所有数量都为0后。设置为无效状态。通过jsnum判断。
						}
					}
					//tmppur.setState(2);//以导入过状态,
					tmppurDAO.attachDirty(tmppur);
				}
			}
		}else{
			//正式库
			List flist = fororderDAO.findByOrderCode(orderCode);
			if(flist!=null && flist.size()>0){
				for(int tl = 0;tl<flist.size();tl++){
					Fororder fororder = (Fororder)flist.get(tl);
					fororder.setIsConvert(1);//以导入过状态
					fororderDAO.attachDirty(fororder);
				}
			}
		}
	}else{
		success =false;
		message = "对不起，入库操作失败！";
		request.setAttribute("message",message);
		request.setAttribute("success",success);
		pageContext.forward("/vm/procure/procureEnd.vm");
		return;
	}
	success =true;
	message = "恭喜您，入库操作成功！";
	request.setAttribute("message",message);
	request.setAttribute("success",success);
	pageContext.forward("/vm/procure/procureEnd.vm");
%>
<%!
	
%>